﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Windows.Controls;
using MaterialDesignThemes.Wpf;
using MaterialDesignThemes.Wpf.Transitions;

namespace Main.Domain
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
        {
            _allItems = GenerateDemoItems(snackbarMessageQueue);
            FilterItems(null);

            MovePrevCommand = new AnotherCommandImplementation(
                _ =>
                {
                    if (!string.IsNullOrWhiteSpace(SearchKeyword))
                        SearchKeyword = string.Empty;

                    SelectedIndex--;
                },
                _ => SelectedIndex > 0);

            MoveNextCommand = new AnotherCommandImplementation(
               _ =>
               {
                   if (!string.IsNullOrWhiteSpace(SearchKeyword))
                       SearchKeyword = string.Empty;

                   SelectedIndex++;
               },
               _ => SelectedIndex < _allItems.Count - 1);
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        private readonly ObservableCollection<DemoItem> _allItems;
        private ObservableCollection<DemoItem>? _demoItems;
        private DemoItem? _selectedItem;
        private int _selectedIndex;
        private string? _searchKeyword;

        public string? SearchKeyword
        {
            get => _searchKeyword;
            set
            {
                _searchKeyword = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SearchKeyword)));
                FilterItems(_searchKeyword);
            }
        }

        public ObservableCollection<DemoItem>? DemoItems
        {
            get => _demoItems;
            set
            {
                _demoItems = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DemoItems)));
            }
        }

        public DemoItem? SelectedItem
        {
            get => _selectedItem;
            set
            {
                if (value == null || value.Equals(_selectedItem)) return;

                _selectedItem = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
            }
        }

        public int SelectedIndex
        {
            get => _selectedIndex;
            set
            {
                _selectedIndex = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedIndex)));
            }
        }

        public AnotherCommandImplementation MovePrevCommand { get; }
        public AnotherCommandImplementation MoveNextCommand { get; }

        private ObservableCollection<DemoItem> GenerateDemoItems(ISnackbarMessageQueue snackbarMessageQueue)
        {
            if (snackbarMessageQueue is null)
                throw new ArgumentNullException(nameof(snackbarMessageQueue));

            return new ObservableCollection<DemoItem>
            {
                new DemoItem("Json2Model", new Json2ModelUC{ DataContext = new Json2ModelViewModel(snackbarMessageQueue) }),
                new DemoItem("表结构", new TablesUC{ DataContext = new TablesUCViewModel() }),
                new DemoItem("测试",new TableBoundUC{ DataContext=new TableBoundUCViewModel(snackbarMessageQueue)}),
                new DemoItem("主题颜色", new ColorTool { DataContext = new ColorToolViewModel() })
            };
        }

        private void FilterItems(string? keyword)
        {
            var filteredItems =
                string.IsNullOrWhiteSpace(keyword) ?
                _allItems :
                _allItems.Where(i => i.Name.ToLower().Contains(keyword!.ToLower()));

            DemoItems = new ObservableCollection<DemoItem>(filteredItems);
        }

        public static object CreateObject()
        {
            object obj = null;

            string text = "AppointmentReg.dll";
            if (!text.ToLower().EndsWith(".dll"))
            {
                text += ".dll";
            }
            string controlName = "AppointmentReg.MainUC";
            try
            {
                obj = System.Reflection.Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "\\" + text).CreateInstance(controlName);
            }
            catch (Exception ex)
            {
                return null;
            }
            return obj;
        }
           
        

    }
}